/**
 * 分享业绩赚取提成业务
 * 导购下单和开卡情况不进行统计
 * Created by kevin on 2018/8/10.
 */

import {
  switchStatus,
  getInfo,
  accessSharedData,
  createSharedChainCode,
  addSharedBizData
} from '../api/index.js'

// 查询分享功能开关状态：2 开，3关
const getSwitchStatus = () => {
  wx.$request(switchStatus)
    .then(({ data }) => {
      wx.$getGlobalData().switchStatus = !!(data.status === 2)
      return Promise.resolve()
    })
    .catch(() => {
      return Promise.resolve()
    })
}

// 用户是否导购且拥有分享码
const checkSharedOpCode = () => {
  let platformAccountId = wx.$getGlobalData().platformAccountId
  if (platformAccountId) {
    return wx
      .$request(getInfo, {
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: { platformAccountId }
      })
      .then(
        ({ data }) => {
          console.info('checkSharedOpCode:', data)
          wx.$getGlobalData().sharedOpCode = data.sharedOpCode
          wx.setStorageSync('HAS_LOAD_SHAREOPCODE', true)
          return Promise.resolve()
        },
        ({ data }) => {
          console.info(data)
          wx.setStorageSync('HAS_LOAD_SWITCHSTATUS', true)
          return Promise.resolve()
        }
      )
  } else {
    wx.setStorageSync('HAS_LOAD_SWITCHSTATUS', true)
    return Promise.resolve()
  }
}

// 上报分享访问记录
const bizAccessSharedData = () => {
  let sharedOpCode = wx.$getGlobalData().sharedOpCode
  let outerSharedCode = wx.$getGlobalData().outerSharedCode
  let platformAccountId = wx.$getGlobalData().platformAccountId
  if (outerSharedCode && outerSharedCode.length > 15 && !sharedOpCode && platformAccountId) {
    wx.$request(accessSharedData, {
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        platformAccountId,
        sharedOpCode: outerSharedCode
      }
    }).then(
      ({ data }) => {
        console.info(data)
        return Promise.resolve()
      },
      ({ data }) => {
        console.info(data)
        return Promise.resolve()
      }
    )
  } else {
    return Promise.resolve()
  }
}

// 生成分享关系链id
const generateSharedChainCode = callabck => {
  let hasAuth = wx.$getGlobalData().userId
  let sharedOpCode = wx.$getGlobalData().sharedOpCode
  let outerSharedCode = wx.$getGlobalData().outerSharedCode

  // 无外部传来分享码情况下通过后端业务取判断最近的分享关系来获取分享链
  if (hasAuth && !sharedOpCode) {
    wx.$request(createSharedChainCode, {
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: { sharedOpCode: outerSharedCode }
    }).then(({ data }) => {
      console.info('generateSharedChainCode:', data)
      wx.$getGlobalData().sharedChainCode = data.sharedChainCode
      callabck && callabck(data.sharedChainCode)
    })
  }
}

// 上报分享业绩数据
const bizShareDataReport = ({ performanceType, performanceId } = {}) => {
  generateSharedChainCode((sharedChainCode) => {
    // performanceType: 1 订单，2 开卡
    // performanceId: 如订单号，开卡无标识
    console.info('bizShareDataReport:', performanceType, performanceId, sharedChainCode)
    wx.$request(addSharedBizData, {
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        sharedChainCode,
        performanceType,
        performanceId
      }
    }).then(({ data }) => {
      console.info(data)
    })
  })
}

export {
  getSwitchStatus,
  checkSharedOpCode,
  generateSharedChainCode,
  bizShareDataReport,
  bizAccessSharedData
}
